<?php
namespace Kuoqi\Cli\Compress;
/**
 * Class Compressjs 压缩js的实现方法
 * @package Kuoqi\Cli\Compress
 */
class Compressjs{

    public $content;
    public function input($content){
        $this->content = $content;
    }
    public function output(){
        $this->analysisNotesa();//处理第一种注释//
        $this->analysisNotesb();//处理第二种注释/*----*/
        $this->analysisSpacen();//将换行符及空格转换为空格/*----*/
        $this->complexCode();//补全一些常用写法
        $this->compleleTitle();//补全标题部分
        return $this->content;
    }

    protected function analysisNotesa(){
        $this->content = preg_replace('/\/\/[\s\S]+?\n/',"\n",$this->content);
    }
    protected function analysisNotesb(){
        $this->content = preg_replace('/\/\*[\s\S]+?(\*\/)/',"",$this->content);
    }
    protected function analysisSpacen(){
        $this->content = preg_replace('/[\s\n]+/'," ",$this->content);
    }
    protected function complexCode(){
        $this->content = preg_replace('/([\}\)])\s*(return|if|this|var)/',"$1;$2",$this->content);
        //$this->content = preg_replace('/([\}\)])\s+([a-zA-Z])/',"$1;$2",$this->content);
    }

    protected function compleleTitle(){
        $this->content = trim($this->content);
        $this->content = "/** \n * compress by kuoqiPHP Cli \n * compress datetime ".date("Y-m-d H:i:s")." \n */\n".$this->content;
    }

}